﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Candy.ViewModel;

namespace Candy
{
    public partial class ProjectPage : Page
    {
        public ProjectPage()
        {
            InitializeComponent();
            this.NavigationCacheMode = System.Windows.Navigation.NavigationCacheMode.Required;
            GeneDataGrid.AddHandler(DataGrid.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnGeneDoubleClick), true); 
        }



        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private void FamilyTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            FamiliesViewModel families = FamilyTree.DataContext as FamiliesViewModel;
            families.SelectedFamily = FamilyTree.SelectedValue as FamilyEditViewModel;

        }

        private void OnFamiliyDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                (FamilyTree.DataContext as FamiliesViewModel).EditFamily.Execute((sender as FrameworkElement).DataContext);
            }
        }

        private void ExpandAll(object sender, RoutedEventArgs e)
        {
            FamilyTree.ExpandAll();
        }

        private void CollapseAll(object sender, RoutedEventArgs e)
        {
            FamilyTree.CollapseAll();
        }

        private const int MOUSE_SENSITIVITY = 300;

        private DateTime _previousClick;

        private void OnGeneDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
               
            }

            DataGrid dg = (sender as DataGrid);
            DateTime current = DateTime.Now;
         
            if (_previousClick != null)
            {
                TimeSpan clickSpan = current - _previousClick;
                if (clickSpan.TotalMilliseconds < MOUSE_SENSITIVITY)
                {
                    (GeneDataGrid.DataContext as GenesViewModel).EditGene.Execute(null);
                }
            }
            _previousClick = current;

        }

        private void OnDrop(object sender, Microsoft.Windows.DragEventArgs e)
        {

        }

        private void TreeViewDragDropTarget_ItemDragStarting(object sender, ItemDragEventArgs e)
        {
            e.Cancel = true;
        }

    }
}
